#!/usr/bin/env bash

# Remove the current ejabberd node in a cluster

# copyright (c) 2010-2015 ProcessOne

# Return Code:
#  0 : groovy baby
# 10 : ejabberdctl not found
# 11 : erl not found
# 12 : erlc not found
# 22 : temporary dir can not be created

function error
{
    echo "Error: $1" >&2
    exit $2
}

[ -z $NO_WARNINGS ] && {
    echo "--------------------------------------------------------------------"
    echo ""
    echo "ejabberd cluster configuration"
    echo ""
    echo "This ejabberd node will be removed from the cluster."
    echo "IMPORTANT: this node will be stopped. At least one other clustered"
    echo "node must be running."
    echo ""
    echo "--------------------------------------------------------------------"
    echo "Press any key to continue, or Ctrl+C to stop now"
    read foo
    echo ""
}
echo ok
exit

PA=/tmp/clustersetup_$$
CTL=$(which ejabberdctl)
[ -x "$CTL" ] || {
  HERE=`which "$0"`
  BASE=`dirname $HERE`/..
  ROOTDIR=`cd $BASE; pwd`
  PATH=$ROOTDIR/bin:$PATH
  PA=$ROOTDIR/clustersetup_$$
  CTL=$(which ejabberdctl)
}
echo "Using commands:"
[ -x "$CTL" ] && echo $CTL || error "can't find ejabberdctl" 10

. $CTL stop 2>/dev/null >/dev/null
ERLC=${ERL}c

[ -x $ERL ] && echo $ERL || error "can't find erl"  11
[ -x $ERLC ] && echo $ERLC || error "can't find erlc" 12
echo ""

$CTL stopped

CLUSTERSETUP=clustersetup
CLUSTERSETUP_ERL=$PA/$CLUSTERSETUP.erl

set -o errexit
set -o nounset

mkdir -p $PA || error "$PA cannot be created" 22
cd $PA
cat <<EOF > $CLUSTERSETUP_ERL
-module($CLUSTERSETUP).

-export([start/0]).

del_table_copy(Table, Node) ->
    case mnesia:del_table_copy(Table, Node) of
    {aborted, Reason} -> io:format("Error: can not remove ~p table: ~p~n", [Table, Reason]);
    _ -> io:format("table ~p removed from cluster~n", [Table])
    end.

del_tables([],_) ->
    ok;
del_tables([schema | Tables], Node) ->
    del_tables(Tables, Node);
del_tables([Table | Tables], Node) ->
    del_table_copy(Table, Node),
    del_tables(Tables, Node).

start() ->
    io:format("~n",[]),
    Removed = node(),
    case mnesia:system_info(running_db_nodes)--[Removed] of
    [] -> io:format("Error: no other node running in the cluster~n");
    Nodes ->
        del_tables(mnesia:system_info(local_tables), Removed),
        mnesia:stop(),
        case rpc:call(hd(Nodes), mnesia, del_table_copy, [schema, Removed]) of
        {badrpc,Reason} -> io:format("Error: can not unregister node ~p from cluster: ~p~n", [Removed, Reason]);
        {aborted,Reason} -> io:format("Error: can not unregister node ~p from cluster: ~p~n", [Removed, Reason]);
        {atomic, ok} ->
            mnesia:delete_schema([Removed]),
            io:format("node ~p removed from cluster~n", [Removed])
        end
    end,
    halt(0).
EOF

$ERLC -o $PA $CLUSTERSETUP_ERL
sh -c "$ERL $NAME $ERLANG_NODE -pa $PA $KERNEL_OPTS -mnesia dir \"\\\"$SPOOL_DIR\\\"\" -s mnesia -s $CLUSTERSETUP start"
cd -
rm -Rf $PA

echo "End."
echo "Check that there is no error in the above messages."
